/**
 * $RCSfile: TestMediaSession.java,v $
 * $Revision: 1.1 $
 * $Date: 08/11/2006
 * <p/>
 * Copyright 2003-2006 Jive Software.
 * <p/>
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.jingle.mediaimpl.testxing;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.jivesoftware.smackx.jingle.JingleSession;
import org.jivesoftware.smackx.jingle.media.JingleMediaSession;
import org.jivesoftware.smackx.jingle.media.PayloadType;
import org.jivesoftware.smackx.jingle.nat.TransportCandidate;

/**
 * This Class implements a complete JingleMediaSession for unit testing.
 * 
 * @author Thiago Camargo
 */
public class TestXingMediaSession extends JingleMediaSession {
	// Do nothing

	/**
	 * Creates a TestMediaSession with defined payload type, remote and local
	 * candidates
	 * 
	 * @param payloadType
	 *            Payload of the jmf
	 * @param remote
	 *            the remote information. The candidate that the jmf will be
	 *            sent to.
	 * @param local
	 *            the local information. The candidate that will receive the jmf
	 * @param locator
	 *            media locator
	 */
	public TestXingMediaSession(final PayloadType payloadType,
			final TransportCandidate remote, final TransportCandidate local,
			final String locator, JingleSession jingleSession) {
		super(payloadType, remote, local, "Test", jingleSession);
		initialize();
	}

	private Socket clientSocket;// send media
	private ServerSocket serSocket;// receive media

	/**
	 * Initialize the screen share channels.
	 */
	public void initialize() {

	}

	/**
	 * Starts transmission and for NAT Traversal reasons start receiving also.
	 */
	public void startTrasmit() {
		try {
			clientSocket = new Socket(this.getRemote().getIp(), this
					.getRemote().getPort());
			TestXingCapture cap = new TestXingCapture(clientSocket);
			cap.start();
		} catch (Exception e) {
		}
	}

	/**
	 * Set transmit activity. If the active is true, the instance should
	 * trasmit. If it is set to false, the instance should pause transmit.
	 * 
	 * @param active
	 *            active state
	 */
	public void setTrasmit(boolean active) {

	}

	/**
	 * For NAT Reasons this method does nothing. Use startTransmit() to start
	 * transmit and receive jmf
	 */
	public void startReceive() {
		// Do nothing
		try {
			serSocket = new ServerSocket(this.getLocal().getPort());
			Socket serviceSocket;

			serviceSocket = serSocket.accept();
			TestXingPlayback player = new TestXingPlayback(serviceSocket);
			player.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Stops transmission and for NAT Traversal reasons stop receiving also.
	 */
	public void stopTrasmit() {

	}

	/**
	 * For NAT Reasons this method does nothing. Use startTransmit() to start
	 * transmit and receive jmf
	 */
	public void stopReceive() {

	}
}
